package com.ruoyi.web.controller.video;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.service.ISysDictDataService;
import com.ruoyi.video.domain.BusActiveCode;
import com.ruoyi.video.domain.vo.ActiveCodeAddParam;
import com.ruoyi.video.service.IActiveCodeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * 激活码 信息操作处理
 * 
 * @author ruoyi
 */
@Api("激活码信息管理")
@RestController
@RequestMapping("/bus/active")
public class BusActiveCodeController extends BaseController {

    @Autowired
    private ISysDictDataService dictDataService;

    @Autowired
    private IActiveCodeService activeCodeService;

    /**
     * 获取激活码列表
     */
    @ApiOperation("获取激活码列表")
    @PreAuthorize("@ss.hasPermi('bus:active:list')")
    @GetMapping("/list")
    public TableDataInfo list() {
        startPage();
        List<BusActiveCode> list = activeCodeService.selectActiveCodeList();
        return getDataTable(list);
    }

    @ApiOperation("导出激活码列表")
    @PreAuthorize("@ss.hasPermi('bus:active:export')")
    @GetMapping("/export")
    public AjaxResult export() {
        List<BusActiveCode> list = activeCodeService.selectActiveCodeList();
        ExcelUtil<BusActiveCode> util = new ExcelUtil<BusActiveCode>(BusActiveCode.class);
        return util.exportExcel(list, "激活码数据");
    }

    /**
     * 根据激活码编号获取详细信息
     */
    @ApiOperation("获取激活码详情")
    @PreAuthorize("@ss.hasPermi('bus:active:query')")
    @GetMapping(value = "/{activeId}")
    public AjaxResult getInfo(@PathVariable Long activeId) {
        return AjaxResult.success(activeCodeService.selectActiveCodeById(activeId));
    }

    /**
     * 新增激活码
     */
    @ApiOperation("保存激活码信息")
    @PreAuthorize("@ss.hasPermi('bus:active:add')")
    @Log(title = "激活码", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody ActiveCodeAddParam param) {
        // 此数据字典中，dictSort: 代表VIP等级, dictLabel: 代表VIP的有效天数(99999代表永久), dictValue: 代表VIP的价格, remark: 代表VIP的购买链接
        SysDictData vipDict = dictDataService.selectDictDataById(param.getDictCode());
        if (vipDict == null || StringUtils.isNull(vipDict.getDictLabel())) {    // 判断VIP的有效天数是否为空，该字段必须是数字，1,7,30,99999
            AjaxResult.error("参数错误或VIP等级设置有误");
        }
        List<BusActiveCode> activeList = new ArrayList<BusActiveCode>();
        for (int i = 0; i < param.getCodeNum(); i++) {
            BusActiveCode activeCode = new BusActiveCode();
            activeCode.setActiveCode(UUID.randomUUID().toString());
            activeCode.setActiveDays(vipDict.getDictLabel());   // VIP时长
            activeCode.setActiveUsed("0");  // 0:未使用，1:已使用
            activeCode.setDictCode(param.getDictCode());
            activeCode.setActivePrice(vipDict.getDictValue());
            activeCode.setCreateBy(SecurityUtils.getUsername());
            activeCode.setUpdateBy(SecurityUtils.getUsername());
            activeList.add(activeCode);
        }
        try {
            if (activeList != null && activeList.size() > 0) {
                activeCodeService.batchInsertActiveCode(activeList);
            }
            return AjaxResult.success();
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 删除激活码
     */
    @ApiOperation("删除激活码信息")
    @PreAuthorize("@ss.hasPermi('bus:active:remove')")
    @Log(title = "激活码", businessType = BusinessType.DELETE)
    @DeleteMapping("/{activeId}")
    public AjaxResult remove(@PathVariable("activeId") Long activeId) {
        return toAjax(activeCodeService.deleteActiveCodeById(activeId));
    }
}
